// Decompiled by DJ v3.9.9.91 Copyright 2005 Atanas Neshkov  Date: 2008-7-8 21:47:20
// Home Page : http://members.fortunecity.com/neshkov/dj.html  - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   DBUtil.java

package com.creawor.imei.base;

import javax.naming.*;
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.Configuration;

public class DBUtil
{

    public DBUtil()
    {
    }

    public static Session currentSession()
        throws HibernateException
    {
        if(sessionFactory == null && !getSystemSessionFactory())
            throw new HibernateException("Exception geting SessionFactory from JNDI (X01) ");
        Session s = (Session)session.get();
        if(s == null || !s.isConnected())
        {
            s = sessionFactory.openSession();
            session.set(s);
        }
        return s;
    }

    public static void closeSession()
        throws HibernateException
    {
        Session s = (Session)session.get();
        session.set(null);
        if(s != null)
            s.close();
    }

    private static boolean getSystemSessionFactory()
    {
        try
        {
            Context inttex = new InitialContext();
            sessionFactory = (SessionFactory)inttex.lookup("HibernateSessionFactory");
        }
        catch(NamingException e)
        {
            try
            {
                sessionFactory = (new Configuration()).configure().buildSessionFactory();
                return true;
            }
            catch(Exception ex)
            {
                throw new RuntimeException((new StringBuilder("Exception building SessionFactory(X02): ")).append(ex.getMessage()).toString());
            }
        }
        return true;
    }

    public static void destroy()
    {
        sessionFactory = null;
    }

    private static SessionFactory sessionFactory = null;
    public static final ThreadLocal session = new ThreadLocal();

}